package com.github.ecbp.store.service.controller.front;

import com.github.ecbp.common.api.CommonResult;
import com.github.ecbp.common.data.builder.CommonPageBuilder;
import com.github.ecbp.common.data.controller.BaseController;
import com.github.ecbp.common.data.utils.TransformUtils;
import com.github.ecbp.common.req.BaseIdDTO;
import com.github.ecbp.common.req.BaseIdListDTO;
import com.github.ecbp.store.service.dto.CouponDTO;
import com.github.ecbp.store.service.dto.CouponQueryDTO;
import com.github.ecbp.store.service.entity.Coupon;
import com.github.ecbp.store.service.service.CouponService;
import com.github.ecbp.store.service.vo.CouponPageVO;
import com.github.ecbp.store.service.vo.CouponVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.github.ecbp.common.constant.CacheKeyConstant.PROJECT_NAME;

/**
 * @Author Created by Alex·He on 2020/5/29 10:00
 * @Version 1.0
 * @Description 优惠卷表(Coupon)表控制层
 */
@Api(tags = "优惠卷管理")
@RequestMapping("/api/" + PROJECT_NAME + "/coupon")
@RestController
public class CouponController extends BaseController {
    @Autowired
    private CouponService service;

    @ApiOperation(value = "添加优惠卷")
    @PostMapping(value = "/create")
    public CommonResult create(@Validated @RequestBody CouponDTO param) {
        service.save(param);
        return CommonResult.succeed();
    }

    @ApiOperation(value = "根据ID删除优惠卷")
    @PostMapping(value = "/delete")
    public CommonResult delete(@Validated(BaseIdDTO.NotNullId.class) @RequestBody BaseIdDTO param) {
        service.removeById(param.getId());
        return CommonResult.succeed();
    }

    @ApiOperation(value = "批量删除优惠卷")
    @PostMapping("/deleteByBatch")
    public CommonResult deleteByBatch(@Validated @RequestBody BaseIdListDTO param) {
        service.removeByIds(param.getIds());
        return CommonResult.succeed();
    }

    @ApiOperation(value = "修改优惠卷")
    @PostMapping(value = "/update")
    public CommonResult update(@Validated(BaseIdDTO.NotNullId.class) @RequestBody CouponDTO param) {
        return CommonResult.succeed(service.updateById(param));
    }

    @ApiOperation(value = "根据ID获取优惠卷详情", response = CouponVO.class)
    @PostMapping(value = "/info")
    public CommonResult getItem(@Validated(BaseIdDTO.NotNullId.class) @RequestBody BaseIdDTO param) {
        return CommonResult.succeed(service.getById(param.getId()));
    }

    @ApiOperation(value = "分页列表-优惠卷", response = CouponPageVO.class)
    @PostMapping(value = "/listAllPage")
    public CommonResult listAllPage(@Validated @RequestBody CouponQueryDTO param) {
        return CommonResult.succeed(CommonPageBuilder.build(
                service.page(TransformUtils.copy(param, Coupon.class)), CouponPageVO.class));
    }
}
